package com.cooper.android.comm;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

public class GalleryTile extends Gallery {

	private float zoomScale = 1.1f;

	private int mCoveflowCenter;

	public GalleryTile(Context context) {
		super(context);
		init();
	}

	public GalleryTile(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public GalleryTile(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.setStaticTransformationsEnabled(true);
		//this.setLa
	}

	private int getCenterOfCoverflow() {
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
				+ getPaddingLeft();
	}

	public float getZoomScale() {
		return zoomScale;
	}

	public void setZoomScale(float zoomScale) {
		this.zoomScale = zoomScale;
	}

	/**
	 * Get the Centre of the View
	 * 
	 * @return The centre of the given view. }
	 */
	private static int getCenterOfView(View view) {
		return view.getLeft() + view.getWidth() / 2;
	}

	@Override
	protected boolean getChildStaticTransformation(View child, Transformation t) {

		final int childCenter = getCenterOfView(child);
		final int childWidth = child.getWidth();
		final int childHeight = child.getHeight();

		t.clear();
		t.setTransformationType(Transformation.TYPE_MATRIX);

		if (childCenter == mCoveflowCenter) {
			Matrix imageMatrix = t.getMatrix();

			imageMatrix.postScale(zoomScale, zoomScale);

			imageMatrix.preTranslate(-(childWidth / 2), -(childHeight / 3));
			imageMatrix.postTranslate((childWidth / 2), (childHeight / 3));

		}

		return true;
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mCoveflowCenter = getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}

}
